package com.itheima.exerciseTest.method10_19.method13;

import java.text.Collator;
import java.util.Locale;

public class Student implements Comparable<Student> {
    private String name;
    private int chinses;
    private int math;
    private int english;

    public Student() {
    }

    public Student(String name, int chinses, int math, int english) {
        this.name = name;
        this.chinses = chinses;
        this.math = math;
        this.english = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinses() {
        return chinses;
    }

    public void setChinses(int chinses) {
        this.chinses = chinses;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", chinses=" + chinses +
                ", math=" + math +
                ", english=" + english +
                '}' + "总分=" + getSum();
    }

    public int getSum(){
        return chinses + math + english;
    }

    @Override
    public int compareTo(Student s) {
        // 比较总分
        int result = this.getSum() - s.getSum();
        if(result != 0)
            return -result;
        // 比较语文
        result = this.chinses - s.chinses;
        if(result != 0)
            return result;
        //比较数学
        result = this.math - s.math;
        if(result != 0)
            return result;
        // 比姓名 典顺序排列 先比英文 没英文就比中文 不能去重
        char[] arr = this.name.toCharArray();
        boolean boo = false;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 'A' && arr[i] <= 'Z') {
                boo = true;
                break;
            } else if (arr[i] >= 'a' && arr[i] <= 'z') {
                boo = true;
                break;
            }
        }
        // 比较英文字典
        if (boo == true) {
            result = this.name.compareTo(s.name);
            return result = result == 0 ? 1 : result;
        }
        // 比较中文字典
        // 创建中文比较器
        Collator ins = Collator.getInstance(Locale.CHINESE);
        result = ins.compare(this.name, s.name);
        if(result == 0)
            this.setName(this.getName() + " 可恶啊");
        return result = result == 0 ? 1 : result;

    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (chinses != student.chinses) return false;
        if (math != student.math) return false;
        if (english != student.english) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + chinses;
        result = 31 * result + math;
        result = 31 * result + english;
        return result;
    }
}
